﻿namespace Bll.Systems
{
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Web.Security;
    using WinFormSession;
    using DALFactory;

    public class UserBll
    {
        private static readonly IUser dal = DataAccess.CreateUser();

        public static void Add(UserEnt model)
        {
            dal.Add(model);
        }

        public static bool CheckUserPassWinform(string UserName, string UserPass)
        {
            UserPass = FormsAuthentication.HashPasswordForStoringInConfigFile(UserPass, "MD5").ToLower();
            DataTable list = dal.GetList("LoginName='" + UserName + "' and UserPass='" + UserPass + "' and IsUseLogin=1");
            if (list.Rows.Count > 0)
            {
                StoreSessionWinform(list.Rows[0]["UserId"].ToString());
                return true;
            }
            return false;
        }

        public static void Delete(int ID)
        {
            dal.Delete(ID);
        }

        public static DataTable GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        public static int GetMaxId(string swhere)
        {
            return dal.GetMaxId(swhere);
        }

        public static UserEnt GetModel(int ID)
        {
            return dal.GetModel(ID);
        }

        public static DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            return dal.GetPageList(PageSize, PageIndex, strWhere);
        }

        public static DataTable SearchData(string sWhere)
        {
            return dal.SearchData(sWhere);
        }

        public static DataTable SearchData(int PageCount, int CurrPageIndex, string lblWhere)
        {
            return dal.SearchData(PageCount, CurrPageIndex, lblWhere);
        }

        public static DataTable SearchSaleData(string lblWhere)
        {
            return dal.SearchSaleData(lblWhere);
        }

        public static DataTable SearchSaleData(int PageCount, int CurrPageIndex, string lblWhere)
        {
            return dal.SearchSaleData(PageCount, CurrPageIndex, lblWhere);
        }

        public static void StoreSessionWinform(string UserId)
        {
            try
            {
                UserEnt model = GetModel(int.Parse(UserId));
                model.UserPass = "";
                WinformSession.GetSession()["user"] = model;
            }
            catch (Exception)
            {
            }
        }

        public static void Update(UserEnt model)
        {
            dal.Update(model);
        }
    }
}

